//
//  PTRecorderDetailViewController.m
//  PocketTutor
//
//  Created by yate1996 on 15/6/10.
//  Copyright (c) 2015年 yate1996. All rights reserved.
//

#import "PTRecorderDetailViewController.h"
#import "PTRecorderLessonTextController.h"
#import "PTRecorderChatController.h"
#import "PTRecorderSetValueController.h"
#import "PTTeacherMessageView.h"
#import "Header.h"

@interface PTRecorderDetailViewController ()
@property (nonatomic, strong) UILabel *teacher;
@property (nonatomic, strong) UIImageView *teacherIcon;
@property (nonatomic, strong) UILabel *teacherName;
@property (nonatomic, strong) UILabel *teacherLevel;
@property (nonatomic, strong) UILabel *myValue;
@property (nonatomic, strong) UILabel *myValueTextLabel;
@property (nonatomic, strong) UIButton *sendMessage;
@property (nonatomic, strong) UIButton *lessonText;
@property (nonatomic, strong) UIImageView *starImageView;//满五星imageView
@property (nonatomic, strong) UIImageView *starEmptyImageView;//空五星imageView
@property (nonatomic, strong) UIView *teacherValueView;
@property (nonatomic, strong) UIView *myCommentView;//我的评价视图
@property (nonatomic, strong) UIView *myValueView;
@property (nonatomic, assign) CGFloat teacherScore;
@property (nonatomic, assign) CGFloat  myValueScore;
@property (nonatomic, weak) UIButton *sendMeMessageButton ;
@property (nonatomic, weak) UIButton *setValueButton;
@property (nonatomic, strong)NSString *string;
@property (nonatomic, strong) PTTeacherMessageView *teacherMessageView;

@end

@implementation PTRecorderDetailViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = PTColor(245, 245, 245, 1);
    self.title = @"Summary";
    
    self.teacherValueView = [self setupSetValue:4.5];
    self.myValueView = [self setupSetValue:_myValueScore];

    [self setupDetail];
    [self setupDetailFrame];
    

}
//星星评分等级
-(UIView *)setupSetValue:(CGFloat  )teacherScore
{
    
    UIView *teacherValue = [[UIView alloc]init];
    
    //空星级imageView
    _starEmptyImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"emptyStars@2x"]];
    _starEmptyImageView.frame = CGRectMake(0,0,_starEmptyImageView.image.size.width,_starEmptyImageView.image.size.height);
    _starEmptyImageView.userInteractionEnabled=YES;
    [teacherValue addSubview:_starEmptyImageView];

    _starImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Stars@2x"]];
    
    _starImageView.frame=CGRectMake(0,0,teacherScore * _starEmptyImageView.width / 5.0,_starEmptyImageView.height);
    _starImageView.contentMode=UIViewContentModeLeft;
    _starImageView.clipsToBounds=YES;
    
    teacherValue.frame = CGRectMake(0, 0, _starEmptyImageView.width, _starEmptyImageView.height);
    [teacherValue addSubview:_starImageView];

    return teacherValue;
}
//约课时间、上课时间、课程名称创建函数
-(UIView *)setuplessViewWithString1:(NSString *)string1 String2:(NSString *)string2
{
    UIView *studyAndLessonView = [[UIView alloc]init];
    studyAndLessonView.backgroundColor = [UIColor whiteColor];
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(13, 0, 320, 44)];
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 307, 44)];
    label1.text = string1;
    label2.text = string2;
    label1.font = PTHelveticaNeueLight12;
    label2.font = PTHelveticaNeueLight12;
    label1.textColor = PTColor(103 , 103, 110, 1.0);
    label2.textAlignment = NSTextAlignmentRight;
    [studyAndLessonView addSubview:label1];
    [studyAndLessonView addSubview:label2];
    return studyAndLessonView;
}

-(void)setupDetail
{
//约课时间、上课时间、课程名称
    UIView *studyAndLessonView1 = [self setuplessViewWithString1:@"Appointment Time:" String2:@"06/12/2015 12:00"];
    UIView *studyAndLessonView2 = [self setuplessViewWithString1:@"Study Time:" String2:@"06/12/2015 18:00-18:25"];
    UIView *studyAndLessonView3 = [self setuplessViewWithString1:@"Lesson:" String2:@"Finish the Sentence"];
    studyAndLessonView1.frame = CGRectMake(0, 74, 320, 44);
    studyAndLessonView2.frame = CGRectMake(0, 119, 320, 44);
    studyAndLessonView3.frame = CGRectMake(0, 164, 320, 44);
    [self.view addSubview:studyAndLessonView1];
    [self.view addSubview:studyAndLessonView2];
    [self.view addSubview:studyAndLessonView3];

//教师名称
    NSMutableDictionary *textAttr = [NSMutableDictionary dictionary];
    textAttr[NSFontAttributeName] = PTHelveticaNeueLight12;
    
    UILabel *teacher = [[UILabel alloc]init];
    teacher.text = @"Teacher";
    teacher.textColor = PTColor(103 , 103, 110, 1.0);
    [self.view addSubview:teacher];
    self.teacher = teacher;
    
//教师简介
    PTTeacherMessageView *teacherMessageView = [[PTTeacherMessageView alloc]init];
    teacherMessageView.meItem = (MeItem *)self.recorder;
    [teacherMessageView.rightArrowButton setImage:nil forState:UIControlStateNormal];
    teacherMessageView.height = teacherMessageView.cellHeight;
    self.teacherMessageView = teacherMessageView;
    [self.view addSubview:teacherMessageView];

//发消息按钮
    UIButton *sendMeMessageButton = [[UIButton alloc]init];
    self.sendMeMessageButton = sendMeMessageButton;
    [self.view addSubview:sendMeMessageButton];
    [self.sendMeMessageButton addTarget:self action:@selector(sendMessageButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
//评价按钮
    UIButton *setValueButton = [[UIButton alloc]init];
    self.setValueButton = setValueButton;
    [self.view addSubview:setValueButton];
    [self.setValueButton addTarget:self action:@selector(setValueButtonClick) forControlEvents:UIControlEventTouchUpInside];

//控件字体
    self.teacher.font = PTHelveticaNeueLight12;
    self.teacherName.font = PTHelveticaNeueLight12;
    self.teacherLevel.font = PTHelveticaNeueLight12;
    self.myValue.font = PTHelveticaNeueLight12;
    
//查看课程文本
    UIButton *lessonText = [[UIButton alloc]init];
    [lessonText setTitle:@"Read Textbook" forState:UIControlStateNormal];
    [lessonText setBackgroundImage:[UIImage imageNamed:@"ReadTextbook"] forState:UIControlStateNormal];
    [self.view addSubview:lessonText];
    self.lessonText = lessonText;
    lessonText.tintColor =[UIColor whiteColor] ;
    self.lessonText.titleLabel.font = PTRecorderFont15;
    [self.lessonText addTarget:self action:@selector(lessonTextButtonClick) forControlEvents:UIControlEventTouchUpInside];
    

}
-(void)setValueButtonClick
{
    PTRecorderSetValueController *setValueView = [[PTRecorderSetValueController alloc]init];
    setValueView.meItem = (MeItem *)self.recorder;;
    [self.navigationController pushViewController:setValueView animated:YES];
    
    setValueView.block = ^ (CGFloat score , NSString *string){
        
    _myValueScore = score;
        self.string = string;
//    _myValueTextLabel.text = string;
    _starImageView.frame = CGRectMake(0,0,_myValueScore * _starEmptyImageView.width / 5.0,_starEmptyImageView.height);
    [self setupDetailFrame];
    };

}
-(void)sendMessageButtonClick
{
    PTRecorderChatController *chatView = [[PTRecorderChatController alloc]init];
    [self.navigationController pushViewController:chatView animated:YES];
}

-(void)lessonTextButtonClick
{
    PTRecorderLessonTextController *lessonTextView = [[PTRecorderLessonTextController alloc]init];
    [self.navigationController pushViewController:lessonTextView animated:YES];
}

-(void)setupDetailFrame
{
//教师名称
    CGFloat teacherY = 208;
    CGFloat teacherX = 13;
    self.teacher.frame = (CGRect){{teacherX,teacherY},320,44};
    
    self.teacherMessageView.frame = CGRectMake(0, CGRectGetMaxY(self.teacher.frame), 320, self.teacherMessageView.height);
//设置发送消息按钮
    UIImage *image = [UIImage imageNamed:@"SendMeMessage"];
    [self.sendMeMessageButton setImage: image forState:UIControlStateNormal];
    self.sendMeMessageButton.frame = CGRectMake(self.view.width - image.size.width - 8 , self.teacherMessageView.centerY - image.size.height - 4 , image.size.width, image.size.height);
//设置评价按钮
    UIImage *image2 = [UIImage imageNamed:@"Comment"];
    [self.setValueButton setImage: image2 forState:UIControlStateNormal];
    self.setValueButton.frame = CGRectMake(self.view.width - image.size.width - 8 , self.teacherMessageView.centerY + 4 , image.size.width, image.size.height);
    
    if(_myValueScore)
    {
        [self.setValueButton removeFromSuperview];
        //        self.setValueButton.frame = CGRectMake(0, 0, 0, 0);
        self.sendMeMessageButton.frame = CGRectMake(self.view.width - image.size.width - 8 , self.teacherMessageView.centerY - image.size.height * 0.5, image.size.width, image.size.height);

//我的评价页面
        [self setupMyComment];
    }

    

    
//查看课程文本
    CGFloat lessonTextX = 13;
    CGFloat lessonTextY = MAX(CGRectGetMaxY(self.myCommentView.frame), CGRectGetMaxY(self.teacherMessageView.frame))  + 13;
    UIImage *lessonTextImage = [UIImage imageNamed:@"ReadTextbook"];
    self.lessonText.frame = (CGRect){{lessonTextX,lessonTextY},self.view.width - 26, lessonTextImage.size.height};


}
-(void)setupMyComment
{
    UIView *myCommentView = [[UIView alloc]init];
    [myCommentView setBackgroundColor :[UIColor whiteColor]];
    [self.view addSubview:myCommentView];

    self.myCommentView = myCommentView;

//我的评价
    UILabel *myValue = [[UILabel alloc]init];
    myValue.textColor = PTColor(103 , 103, 110, 1.0);
    myValue.font = PTHelveticaNeueLight12;
    myValue.text = @"My Comments";
    myValue.frame = CGRectMake(13, 0, 320, 38);
    [myCommentView addSubview:myValue];
    self.myValue = myValue;
    
//我的星星评分
//    self.myValueView = [self setupSetValue:4.5];
    self.myValueView.frame = CGRectMake(13, CGRectGetMaxY(myValue.frame), self.myValueView.width,self.myValueView.height);
    [myCommentView addSubview:self.myValueView];
    
//教师评价文本
    NSString *string = self.string;
    UILabel *myValueTextLabel = [[UILabel alloc]init];
    myValueTextLabel.textColor = PTColor(88 , 90, 96, 1.0);
    myValueTextLabel.font = [UIFont systemFontOfSize:13];
    myValueTextLabel.numberOfLines = 0;
    NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:8];
    [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [string length])];
    [myValueTextLabel setAttributedText:attributedString1];
    myValueTextLabel.frame = (CGRect){{13 , CGRectGetMaxY(self.myValueView.frame) +5 },{ 298 , 0}};
    [myValueTextLabel sizeToFit];
    [myCommentView addSubview:myValueTextLabel];
    self.myValueTextLabel = myValueTextLabel;
    myCommentView.frame = CGRectMake(0, CGRectGetMaxY(self.teacherMessageView.frame) + 1 , 320, CGRectGetMaxY(self.myValueTextLabel.frame) + 13);

}

@end
